# coding: utf-8
from sqlalchemy import BigInteger, Column, Date, DateTime, ForeignKey, Integer, Numeric, String
from sqlalchemy.orm import relationship
from sqlalchemy.schema import FetchedValue
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()


class AddressInfo(db.Model):
    __tablename__ = 'address_info'

    id = db.Column(db.BigInteger, primary_key=True)
    province_code = db.Column(db.String(10), nullable=False)
    city_code = db.Column(db.String(10), nullable=False)
    district_code = db.Column(db.String(10))
    address_detail = db.Column(db.String(255), nullable=False)
    postcode = db.Column(db.String(10), nullable=False)
    name = db.Column(db.String(255), nullable=False)
    mobile = db.Column(db.String(20), nullable=False)
    user_id = db.Column(db.ForeignKey(u'user_info.id'), nullable=False, index=True)

    user = db.relationship(u'UserInfo', primaryjoin='AddressInfo.user_id == UserInfo.id', backref=u'address_infos')


class CarInfo(db.Model):
    __tablename__ = 'car_info'

    id = db.Column(db.BigInteger, primary_key=True)
    license_no = db.Column(db.String(20), nullable=False)
    vin_no = db.Column(db.String(20), nullable=False)
    engine_no = db.Column(db.String(20), nullable=False)
    model_name = db.Column(db.String(255), nullable=False)
    register_date = db.Column(db.Date, nullable=False)
    is_tran = db.Column(db.Integer, nullable=False)
    trans_date = db.Column(db.Date)
    customer_id = db.Column(db.ForeignKey(u'customer_info.id'), nullable=False, index=True)
    model_id = db.Column(db.ForeignKey(u'car_model_info.id'), nullable=False, index=True)
    driving_license_img_URL = db.Column(db.String(255), server_default=db.FetchedValue())
    driving_license_img_back_URL = db.Column(db.String(255), server_default=db.FetchedValue())

    customer = db.relationship(u'CustomerInfo', primaryjoin='CarInfo.customer_id == CustomerInfo.id', backref=u'car_infos')
    model = db.relationship(u'CarModelInfo', primaryjoin='CarInfo.model_id == CarModelInfo.id', backref=u'car_infos')


class CarModelInfo(db.Model):
    __tablename__ = 'car_model_info'

    id = db.Column(db.BigInteger, primary_key=True)
    type_name = db.Column(db.String(100), nullable=False)
    vehicleFgwCode = db.Column(db.String(50), nullable=False)
    brandCode = db.Column(db.String(50), nullable=False)
    engineDesc = db.Column(db.String(20), nullable=False)
    familyName = db.Column(db.String(255), nullable=False)
    gearboxType = db.Column(db.String(100))
    remark = db.Column(db.String(255))
    newCarPrice = db.Column(db.String(20))
    purchasePriceTax = db.Column(db.String(20))
    importFlag = db.Column(db.String(10))
    purchasePrice = db.Column(db.String(18))
    seat = db.Column(db.String(10))
    standardName = db.Column(db.String(255))


class CityInfo(db.Model):
    __tablename__ = 'city_info'

    id = db.Column(db.Integer, primary_key=True)
    cityName = db.Column(db.String(40), nullable=False)
    provinceCode = db.Column(db.String(20), nullable=False, index=True)
    gbCode = db.Column(db.String(20), nullable=False)


class CoverageList(db.Model):
    __tablename__ = 'coverage_list'

    id = db.Column(db.Integer, primary_key=True)
    riskCode = db.Column(db.String(50), nullable=False)
    riskName = db.Column(db.String(150), nullable=False)
    insuredAmount = db.Column(db.String(600), nullable=False)
    status = db.Column(db.String(255), server_default=db.FetchedValue())


class CustomerInfo(db.Model):
    __tablename__ = 'customer_info'

    id = db.Column(db.BigInteger, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    identity_no = db.Column(db.String(20), nullable=False)
    mobile = db.Column(db.String(20), nullable=False)
    user_id = db.Column(db.ForeignKey(u'user_info.id'), index=True)
    identity_front_url = db.Column(db.String(80), server_default=db.FetchedValue())
    identity_back_url = db.Column(db.String(80), server_default=db.FetchedValue())
    pinyincode = db.Column(db.String(20), server_default=db.FetchedValue())

    user = db.relationship(u'UserInfo', primaryjoin='CustomerInfo.user_id == UserInfo.id', backref=u'customer_infos')


class DistrictInfo(db.Model):
    __tablename__ = 'district_info'

    id = db.Column(db.Integer, primary_key=True)
    districtName = db.Column(db.String(40), nullable=False)
    cityCode = db.Column(db.String(20), index=True)
    gbcode = db.Column(db.String(20), nullable=False)


class InsuranceOrder(db.Model):
    __tablename__ = 'insurance_order'

    id = db.Column(db.BigInteger, primary_key=True)
    car_id = db.Column(db.ForeignKey(u'car_info.id'), nullable=False, index=True)
    customer_id = db.Column(db.ForeignKey(u'customer_info.id'), nullable=False, index=True)
    user_id = db.Column(db.ForeignKey(u'user_info.id'), nullable=False, index=True)
    bizId = db.Column(db.String(40), nullable=False)
    insurance_code = db.Column(db.String(20), nullable=False)
    city_code = db.Column(db.String(20), nullable=False)
    district_code = db.Column(db.String(20), nullable=False)
    total_cost = db.Column(db.Numeric(15, 2))
    ciProposalNo = db.Column(db.String(40))
    biProposalNo = db.Column(db.String(40))
    ciPolicyNo = db.Column(db.String(40))
    biPolicyNo = db.Column(db.String(40))
    ci_start_date = db.Column(db.Date)
    ci_end_date = db.Column(db.Date)
    bi_start_date = db.Column(db.Date)
    bi_end_date = db.Column(db.Date)
    content = db.Column(db.String)
    oder_time = db.Column(db.DateTime, nullable=False)
    status = db.Column(db.Integer, nullable=False)
    qry_id = db.Column(db.ForeignKey(u'insurance_qry.id'), nullable=False, index=True)
    msg = db.Column(db.String(500))
    ciPremium = db.Column(db.Numeric(15, 2))
    biPremium = db.Column(db.Numeric(15, 2))
    carship_tax = db.Column(db.Numeric(15, 2))
    discount_rate = db.Column(db.Numeric(15, 2))
    discount = db.Column(db.Numeric(15, 2))
    addressee_name = db.Column(db.String(255), nullable=False)
    postcode = db.Column(db.String(10))
    addressee_detail = db.Column(db.String(255), nullable=False)
    addressee_mobile = db.Column(db.String(20), nullable=False)
    addressee_province = db.Column(db.String(10), nullable=False)
    addressee_city = db.Column(db.String(10), nullable=False)
    addressee_district = db.Column(db.String(10))

    car = db.relationship(u'CarInfo', primaryjoin='InsuranceOrder.car_id == CarInfo.id', backref=u'insurance_orders')
    customer = db.relationship(u'CustomerInfo', primaryjoin='InsuranceOrder.customer_id == CustomerInfo.id', backref=u'insurance_orders')
    qry = db.relationship(u'InsuranceQry', primaryjoin='InsuranceOrder.qry_id == InsuranceQry.id', backref=u'insurance_orders')
    user = db.relationship(u'UserInfo', primaryjoin='InsuranceOrder.user_id == UserInfo.id', backref=u'insurance_orders')


class InsuranceQry(db.Model):
    __tablename__ = 'insurance_qry'

    id = db.Column(db.BigInteger, primary_key=True)
    car_id = db.Column(db.ForeignKey(u'car_info.id'), nullable=False, index=True)
    customer_id = db.Column(db.ForeignKey(u'customer_info.id'), nullable=False, index=True)
    user_id = db.Column(db.ForeignKey(u'user_info.id'), nullable=False, index=True)
    total_cost = db.Column(db.Numeric(15, 2))
    city_code = db.Column(db.String(15), nullable=False)
    district_code = db.Column(db.String(15), nullable=False)
    qry_time = db.Column(db.DateTime, nullable=False)
    ci_start_date = db.Column(db.Date, nullable=False)
    bi_start_date = db.Column(db.Date, nullable=False)
    biz_id = db.Column(db.String(40), nullable=False)
    insurance_detail = db.Column(db.String(255))
    insurance_code = db.Column(db.String(20), nullable=False)
    error_msg = db.Column(db.String(500))
    status = db.Column(db.Integer, server_default=db.FetchedValue())

    car = db.relationship(u'CarInfo', primaryjoin='InsuranceQry.car_id == CarInfo.id', backref=u'insurance_qries')
    customer = db.relationship(u'CustomerInfo', primaryjoin='InsuranceQry.customer_id == CustomerInfo.id', backref=u'insurance_qries')
    user = db.relationship(u'UserInfo', primaryjoin='InsuranceQry.user_id == UserInfo.id', backref=u'insurance_qries')


class InsuranceUnit(db.Model):
    __tablename__ = 'insurance_unit'

    id = db.Column(db.Integer, primary_key=True)
    insurance_code = db.Column(db.String(20), nullable=False)
    insurance_name = db.Column(db.String(20), nullable=False)
    logoUrl = db.Column(db.String(255))
    status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())


class MyBankInfo(db.Model):
    __tablename__ = 'my_bank_info'

    id = db.Column(db.BigInteger, primary_key=True)
    user_id = db.Column(db.ForeignKey(u'user_info.id'), nullable=False, index=True)
    bank_name = db.Column(db.String(20), nullable=False)
    bank_card = db.Column(db.String(20), nullable=False)
    opening_bank = db.Column(db.String(255), nullable=False)
    is_valid = db.Column(db.Integer, nullable=False)
    username = db.Column(db.String(100), nullable=False)

    user = db.relationship(u'UserInfo', primaryjoin='MyBankInfo.user_id == UserInfo.id', backref=u'my_bank_infos')


class ProvinceInfo(db.Model):
    __tablename__ = 'province_info'

    id = db.Column(db.Integer, primary_key=True)
    provinceName = db.Column(db.String(20), nullable=False)
    briefName = db.Column(db.String(20), nullable=False)
    gbcode = db.Column(db.String(20), nullable=False)


class QryCoverageList(db.Model):
    __tablename__ = 'qry_coverage_list'

    id = db.Column(db.BigInteger, primary_key=True)
    qry_id = db.Column(db.ForeignKey(u'insurance_qry.id'), nullable=False, index=True)
    cost = db.Column(db.Numeric(15, 2), nullable=False)
    insurance_code = db.Column(db.String(40), nullable=False)
    warranty_cost = db.Column(db.Numeric(10, 2))

    qry = db.relationship(u'InsuranceQry', primaryjoin='QryCoverageList.qry_id == InsuranceQry.id', backref=u'qry_coverage_lists')


class SupportCommission(db.Model):
    __tablename__ = 'support_commission'

    id = db.Column(db.Integer, primary_key=True)
    city_code = db.Column(db.String(20), nullable=False)
    insurance_code = db.Column(db.String(20), nullable=False)
    rate1 = db.Column(db.Numeric(15, 2), nullable=False, server_default=db.FetchedValue())
    rate2 = db.Column(db.Numeric(15, 2), nullable=False, server_default=db.FetchedValue())
    rate3 = db.Column(db.Numeric(15, 2), nullable=False, server_default=db.FetchedValue())
    rate4 = db.Column(db.Numeric(15, 2), nullable=False, server_default=db.FetchedValue())


class TransHistory(db.Model):
    __tablename__ = 'trans_history'

    id = db.Column(db.BigInteger, primary_key=True)
    trans_type = db.Column(db.Integer, nullable=False)
    user_id = db.Column(db.ForeignKey(u'user_info.id'), nullable=False, index=True)
    order_id = db.Column(db.ForeignKey(u'insurance_order.id'), index=True)
    bank_card = db.Column(db.String(20))
    trans_time = db.Column(db.DateTime)
    description = db.Column(db.String(255), nullable=False)
    money = db.Column(db.Numeric(15, 2), nullable=False)
    bank_name = db.Column(db.String(80))

    order = db.relationship(u'InsuranceOrder', primaryjoin='TransHistory.order_id == InsuranceOrder.id', backref=u'trans_histories')
    user = db.relationship(u'UserInfo', primaryjoin='TransHistory.user_id == UserInfo.id', backref=u'trans_histories')


class UserInfo(db.Model):
    __tablename__ = 'user_info'

    id = db.Column(db.BigInteger, primary_key=True)
    mobile = db.Column(db.String(255), nullable=False, unique=True)
    name = db.Column(db.String(255))
    pwd = db.Column(db.String(255))
    pay_pass = db.Column(db.String(255))
    balance = db.Column(db.Numeric(15, 2), nullable=False, server_default=db.FetchedValue())
    user_uuid = db.Column(db.String(50), nullable=False)
    pid = db.Column(db.BigInteger, index=True)
    is_lock = db.Column(db.Integer, index=True, server_default=db.FetchedValue())


class WeixinUser(db.Model):
    __tablename__ = 'weixin_user'

    id = db.Column(db.BigInteger, primary_key=True)
    open_id = db.Column(db.String(80), nullable=False)
    nickName = db.Column(db.String(100))
    sex = db.Column(db.Integer)
    city = db.Column(db.String(80))
    country = db.Column(db.String(80))
    province = db.Column(db.String(80))
    language = db.Column(db.String(80))
    headimgurl = db.Column(db.String(255))
    subscribe = db.Column(db.Integer)
    user_id = db.Column(db.ForeignKey(u'user_info.id'), index=True)

    user = db.relationship(u'UserInfo', primaryjoin='WeixinUser.user_id == UserInfo.id', backref=u'weixin_users')
